﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace System.Windows.Forms
{
    /// <summary>
    /// 表示一种具有PlaceHolder特性的文本控件
    /// </summary>
    /// <creator>marc</creator>
    public class PlaceHolderTextBox : TextBox
    {
        /// <inheritdoc cref="PlaceHolderTextBox"/>
        public PlaceHolderTextBox()
        {

        }

        /// <summary>
        /// 提示文字
        /// </summary>
        [Browsable(true)]
        [Category("Xejen")]
        [Description("提示文字")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string EmptyTextPrompt { get; set; }

        /// <summary>
        /// 提示文字的颜色
        /// </summary>
        [Browsable(true)]
        [Category("Xejen")]
        [Description("提示文字的颜色")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Color EmptyTextPromptColor { get; set; } = Color.LightGray;

        /// <inheritdoc/>
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            base.WndProc(ref m);
            if ((m.Msg == 0xF || m.Msg == 0x133) && isPlaceHolderVisible)
            {
                WmPaint(ref m);
            }
        }

        /// <summary>
        /// 标记：当光标落在框内，提示文字消息。当光标不在时，显示提示文字（若没有内容时）
        /// </summary>
        private bool isPlaceHolderVisible = true;

        /// <inheritdoc/>
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            if (string.IsNullOrEmpty(this.Text))
            {
                isPlaceHolderVisible = false;
                this.Invalidate();
            }
        }

        /// <inheritdoc/>
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            if (string.IsNullOrEmpty(this.Text))
            {
                isPlaceHolderVisible = true;
                this.Invalidate();
            }
        }

        /// <inheritdoc/>
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            if (!string.IsNullOrEmpty(this.Text))
            {
                isPlaceHolderVisible = false;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 绘制的矩形位置
        /// </summary>
        protected Rectangle Position { get; set; } = new Rectangle(0, 0, 0, 0);

        /// <summary>
        /// 绘制提示文字
        /// </summary>
        /// <param name="message"></param>
        protected virtual void WmPaint(ref Message message)
        {
            Graphics g = Graphics.FromHwnd(base.Handle);
            if (!String.IsNullOrEmpty(this.EmptyTextPrompt))
            {
                using (var brush = new SolidBrush(this.EmptyTextPromptColor))
                {
                    SizeF textSize = g.MeasureString(this.EmptyTextPrompt, this.Font);
                    float x = 0;
                    float y = (this.ClientSize.Height - textSize.Height) / 2;
                    g.DrawString(this.EmptyTextPrompt, this.Font, brush, x, y);
                }
            }
        }
    }
}